package com.sit.estatemanagementserver.dao;

import com.sit.estatemanagementserver.enity.Facility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class FacilityDao {
    @Autowired
    DatabaseManager db;

    // 无需重构, Facility实体未变
    private Facility resultSetToFacility(ResultSet rs) throws SQLException {
        return new Facility(
                rs.getString(DatabaseContract.FacilityEntry.COLUMN_FACILITY_ID),
                rs.getString(DatabaseContract.FacilityEntry.COLUMN_FACILITY_NAME)
        );
    }

    public boolean addFacility(Facility facility) {
        String sql = "INSERT INTO " + DatabaseContract.FacilityEntry.TABLE_NAME + " (" +
                DatabaseContract.FacilityEntry.COLUMN_FACILITY_ID + ", " +
                DatabaseContract.FacilityEntry.COLUMN_FACILITY_NAME + ") VALUES (?, ?)";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, facility.getFacilityId());
            pstmt.setString(2, facility.getFacilityName());
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error adding facility '" + facility.getFacilityId() + "': " + e.getMessage());
            return false;
        }
    }

    public Facility getFacility(String facilityId) {
        String sql = "SELECT * FROM " + DatabaseContract.FacilityEntry.TABLE_NAME + " WHERE " +
                DatabaseContract.FacilityEntry.COLUMN_FACILITY_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, facilityId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return resultSetToFacility(rs);
                }
            }
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error getting facility '" + facilityId + "': " + e.getMessage());
        }
        return null;
    }

    public List<Facility> getAllFacilities() {
        List<Facility> facilities = new ArrayList<>();
        String sql = "SELECT * FROM " + DatabaseContract.FacilityEntry.TABLE_NAME;
        try (Connection conn = db.connect();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                facilities.add(resultSetToFacility(rs));
            }
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error getting all facilities: " + e.getMessage());
        }
        return facilities;
    }

    public boolean updateFacility(Facility facility) {
        String sql = "UPDATE " + DatabaseContract.FacilityEntry.TABLE_NAME + " SET " +
                DatabaseContract.FacilityEntry.COLUMN_FACILITY_NAME + " = ? WHERE " +
                DatabaseContract.FacilityEntry.COLUMN_FACILITY_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, facility.getFacilityName());
            pstmt.setString(2, facility.getFacilityId());
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error updating facility '" + facility.getFacilityId() + "': " + e.getMessage());
            return false;
        }
    }

    public boolean deleteFacility(String facilityId) {
        String sql = "DELETE FROM " + DatabaseContract.FacilityEntry.TABLE_NAME + " WHERE " +
                DatabaseContract.FacilityEntry.COLUMN_FACILITY_ID + " = ?";
        try (Connection conn = db.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, facilityId);
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println(DatabaseManager.TAG + ": Error deleting facility '" + facilityId + "': " + e.getMessage());
            return false;
        }
    }
}